package com.walter.quickstart;

import javax.json.bind.annotation.JsonbProperty;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.util.ArrayList;
import java.util.List;

@Path("/developer")
public class DeveloperResource {

    private static final List<Developer> developers = new ArrayList<>();

    @POST
    //@Produces(MediaType.APPLICATION_JSON)
    @Path("/createDeveloper")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createDeveloper(Developer developer) {
        developer.persist();

        return Response.created(UriBuilder
                        .fromResource(DeveloperResource.class)
                        .path(Long.toString(developer.getId()))
                        .build()
                )
                .entity(developer)
                .build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addDeveloper(@Valid Developer developer){
        developers.add(developer);
        return Response.ok().build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Developer> getDevelopers(){
        return developers;
    }

    public static class Developer {
        static long counter = 1;
        private long id;
        @NotBlank
        private String name;
        @JsonbProperty("favorite-lang")
        private String favoriteLang;

        public String getFavoriteLang() {
            return favoriteLang;
        }

        public void setFavoriteLang(String favoriteLang) {
            this.favoriteLang = favoriteLang;
        }

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void persist() {
            this.id = counter++;
        }
    }
}
